package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.pojo.*;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

@Reference
private UserService userService;
@Reference
private MenuService menuService;


    @RequestMapping("/main")
    public Result getMainHtml(){
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            com.itheima.pojo.User user1 = userService.findByUsername(username);
            Set<Role> roles = user1.getRoles();
            List<Menu> menuList= menuService.findMainMenu();
            List<Menu> menus=new ArrayList<>();
             if(menuList!=null && menuList.size()>0){
                 for (Menu menu : menuList) {
                     Set<Role> roles1 = menu.getRoles();
                     if(roles1==null || roles1.size()==0){
                         menus.add(menu);
                     }else {
                         if(roles==null || roles.size()==0){
                             break;
                         }
                         for (Role role : roles1) {
                             for (Role role1 : roles) {
                                 if(role1.getName().equals(role.getName())){
                                     menus.add(menu);
                                     break;
                                 }
                             }
                         }
                     }
                 }
             }

             if(menus!=null && menus.size()>0){
                 for (Menu menu : menus) {

                   List<Menu> childMenus=  menuService.findChilds("/"+menu.getPath());
                   List<Menu> cMenus=new ArrayList<>();
                   if(childMenus!=null && childMenus.size()>0){
                       for (Menu childMenu : childMenus) {
                           Set<Role> roles1 = childMenu.getRoles();
                           if(roles1==null || roles1.size()==0){
                              cMenus.add(childMenu);
                           }else {
                               if(roles==null || roles.size()==0){
                                   break;
                               }
                               for (Role role : roles1) {
                                   for (Role role1 : roles) {
                                       if(role1.getName().equals(role.getName())){
                                           cMenus.add(childMenu);
                                           break;
                                       }
                                   }
                               }
                           }


                       }
                       menu.setChildren(cMenus);

                   }
                 }
               ;
             }

             List<Map> mapList=new ArrayList<>();
             Map map=null;
             Map mapChild=null;
             List<Map> childmap1=null;

            for (Menu menu : menus) {
                map=new HashMap();
                String path = menu.getPath();
                String name = menu.getName();
                String icon = menu.getDescription();
                List<Menu> children = menu.getChildren();
                if(children != null && children.size()>0){
                    childmap1=new ArrayList<>();
                    for (Menu child : children) {

                        mapChild=new HashMap();
                        String cpath = child.getPath();
                        String cname = child.getName();
                        String linkUrl = child.getLinkUrl();
                        mapChild.put("path",cpath);
                        mapChild.put("title",cname);
                        mapChild.put("linkUrl",linkUrl);
                        mapChild.put("children",new ArrayList<>());
                        childmap1.add(mapChild);
                    }


                }
                map.put("path",path);
                map.put("title",name);
                map.put("icon",icon);
                map.put("children",childmap1);

                mapList.add(map);
            }



            return new Result(true,"查询用户可访问菜单成功",mapList);
        }catch (Exception e){
            return new Result(false,"获取主页信息失败!");
        }
    }


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
           PageResult result= menuService.findPage(queryPageBean);
           return result;
        }catch (Exception e){
            return null;
        }

    }


    @RequestMapping("/findAllRole")
    public Result findAllRole(){
        try {
          List<Role> roleList=   menuService.findAllRole();
          return new Result(true,"查询角色列表成功!",roleList);
        }catch (Exception e){
            return new Result(false,"查询角色列表失败!");
        }
    }


    @RequestMapping("/findRolesIdByMenuId")
    public Result findRolesIdByMenuId(Integer menuId){
        try {
            List<Integer> integerList=   menuService.findRolesIdByMenuId(menuId);
            return new Result(true,"查询菜单关联角色ID成功!",integerList);
        }catch (Exception e){
            return new Result(false,"查询菜单关联角色ID失败!");
        }
    }


    @RequestMapping("/updateRoleByMenuId")
    public Result updateRoleByMenuId(Integer menuId,Integer[] roleIds){
        try {
         menuService.updateRoleByMenuId(menuId,roleIds);
            return new Result(true,"菜单更新关联角色信息成功!");
        }catch (Exception e){
            return new Result(false,"菜单更新关联角色信息失败!");
        }
    }

    @RequestMapping("/CreateNewMenu")
    public Result CreateNewMenu(@RequestBody Menu menu){
        try {
            menuService.CreateNewMenu(menu);
            return new Result(true,"新建菜单成功!");
        }catch (Exception e){
            return new Result(false,"新建菜单成功!");
        }
    }

    @RequestMapping("/findMenuById")
    public Result findMenuById(Integer menuId){
        try {
           Menu menu= menuService.findMenuById(menuId);
            return new Result(true,"查找菜单信息成功!",menu);
        }catch (Exception e){
            return new Result(false,"查找菜单信息失败!");
        }
    }


    @RequestMapping("/editMenu")
    public Result editMenu(@RequestBody Menu menu){

        try {
            menuService.editMenu(menu);
            return new Result(true,"修改菜单信息成功!");
        }catch (Exception e){
            return new Result(false,"修改菜单信息失败!");
        }

    }


}
